package org.docmanagement.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.docmanagement.commons.struts.CommonAction;
import org.docmanagement.data.Keyword;
import org.docmanagement.forms.IdForm;
import org.docmanagement.forms.KeywordForm;

/**
 * Created on 11 oct. 2004
 * 
 * @author Mektoub
 */
public class KeywordAction extends CommonAction
{
    /**
     * @see org.docmanagement.commons.struts.BaseDispatchAction#createMethodMap()
     */
    protected void createMethodMap()
    {
        methodMap = new HashMap<String, String>();
        methodMap.put("Lister", "list");
        methodMap.put("Ajouter", "add");
        methodMap.put("Detail", "get");
        methodMap.put("Sauver", "update");
        methodMap.put("Renommer", "get");
        methodMap.put("Supprimer", "delete");
    }
    /**
     * @see org.docmanagement.actions.DataManagerAction#add(org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        KeywordForm keywordForm = (KeywordForm) form;
        log.debug("Adding keyword: " + keywordForm.toString());
        Keyword keyword = toKeyword(keywordForm);
        getKeywordService().add(keyword);
        return mapping.findForward(SUCCESS);
    }
    /**
     * @see org.docmanagement.actions.DataManagerAction#get(org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    public ActionForward get(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        IdForm id = (IdForm) form;
        addKeyword(request, id.getId());
        return mapping.findForward(EDIT);
    }
    /**
     * @see org.docmanagement.actions.DataManagerAction#update(org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        KeywordForm keywordForm = (KeywordForm) form;
        Keyword keyword = toKeyword(keywordForm);
        getKeywordService().update(keyword);
        return mapping.findForward(SUCCESS);
    }
    /**
     * @see org.docmanagement.actions.DataManagerAction#delete(org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        IdForm id = (IdForm) form;
        getKeywordService().removeById(id.getId());
        return mapping.findForward(SUCCESS);
    }
    /**
     * @see org.docmanagement.actions.DataManagerAction#delete(org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        addKeywordList(request);
        return mapping.findForward(SUCCESS);
    }
}